package com.liaogui.web.verify;

import com.liaogui.order.api.domain.ActInfo;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 责任链启动器
 * @author liaogui
 * @date 2025/3/1 21:15
 */
@Component
@Slf4j
public class VerifyStart implements CommandLineRunner, ApplicationContextAware {

    private Map<String, VerifyHandler> verifyHandlerMap;

    private ApplicationContext applicationContext;

    public void handler(ActInfo actInfo) {
        for (Map.Entry<String, VerifyHandler> entry : verifyHandlerMap.entrySet()) {
            VerifyHandler verifyHandler = entry.getValue();
            verifyHandler.operator(actInfo);
        }
    }

    @Override
    public void run(String... args) throws Exception {
        this.verifyHandlerMap = applicationContext.getBeansOfType(VerifyHandler.class);
        log.info("校验责任链处理器加载完成");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
